load("@cmake_configure_file//:cmake_configure_file.bzl", "cmake_configure_file")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

package(default_visibility = ["//visibility:public"])

# Define the required headers to be generated.
cmake_configure_file(
    name = "config_header",
    src = "cmake/config.hh.cmake",
    out = "include/pinocchio/config.hpp",
    cmakelists = ["CMakeLists.txt"],
    defines = [
        "LIBRARY_NAME=" + module_name().upper(),
        "PROJECT_VERSION=" + module_version(),
        "PROJECT_VERSION_MAJOR_CONFIG=" + module_version().split(".")[0],
        "PROJECT_VERSION_MINOR_CONFIG=" + module_version().split(".")[1],
        "PROJECT_VERSION_PATCH_CONFIG=" + module_version().split(".")[2],
        "EXPORT_SYMBOL=" + module_name().upper() + "_EXPORTS",
    ],
    visibility = ["//visibility:private"],
)

cmake_configure_file(
    name = "deprecated_header",
    src = "cmake/deprecated.hh.cmake",
    out = "include/pinocchio/deprecated.hpp",
    cmakelists = ["CMakeLists.txt"],
    defines = [
        "PACKAGE_CPPNAME=" + module_name().upper(),
    ],
    visibility = ["//visibility:private"],
)

cmake_configure_file(
    name = "warning_header",
    src = "cmake/warning.hh.cmake",
    out = "include/pinocchio/warning.hpp",
    cmakelists = ["CMakeLists.txt"],
    defines = [
        "PACKAGE_CPPNAME=" + module_name().upper(),
    ],
    visibility = ["//visibility:private"],
)

filegroup(
    name = "pinocchio_models",
    srcs = glob(["models/**/*"]),
)

cc_library(
    name = "pinocchio",
    srcs = glob(["src/**/*.cpp"]),
    hdrs = glob(
        [
            "include/**/*.hpp",
            "include/**/*.hxx",
        ],
        exclude = ["include/pinocchio/bindings/**/*"],
    ) + [
        ":config_header",
        ":deprecated_header",
        ":warning_header",
    ],
    copts = ["-DPINOCCHIO_URDFDOM_USE_STD_SHARED_PTR"],
    includes = ["include/"],
    deps = [
        "@boost.algorithm//:boost.algorithm",
        "@boost.asio//:boost.asio",
        "@boost.bind//:boost.bind",
        "@boost.filesystem//:boost.filesystem",
        "@boost.foreach//:boost.foreach",
        "@boost.format//:boost.format",
        "@boost.fusion//:boost.fusion",
        "@boost.integer//:boost.integer",
        "@boost.iostreams//:boost.iostreams",
        "@boost.math//:boost.math",
        "@boost.property_tree//:boost.property_tree",
        "@boost.serialization//:boost.serialization",
        "@boost.type_traits//:boost.type_traits",
        "@boost.variant//:boost.variant",
        "@eigen",
        "@sdformat//:urdf_parser",
    ],
)

# Alias for backwards compatibility
alias(
    name = "pinocchio_includes",
    actual = ":pinocchio",
)

BINARY_DEPS = [
    ":pinocchio",
    "@eigen//:eigen",
]

# Core examples
[
    cc_binary(
        name = name,
        srcs = [src],
        deps = BINARY_DEPS,
    )
    for name, src in {
        "build-reduced-model": "examples/build-reduced-model.cpp",
        "forward-dynamics-derivatives": "examples/forward-dynamics-derivatives.cpp",
        "geometry-models": "examples/geometry-models.cpp",
        "interpolation-SE3": "examples/interpolation-SE3.cpp",
        "inverse-dynamics-derivatives": "examples/inverse-dynamics-derivatives.cpp",
        "inverse-kinematics": "examples/inverse-kinematics.cpp",
        "kinematics-derivatives": "examples/kinematics-derivatives.cpp",
        "overview-SE3": "examples/overview-SE3.cpp",
        "overview-lie": "examples/overview-lie.cpp",
        "overview-simple": "examples/overview-simple.cpp",
        "overview-urdf": "examples/overview-urdf.cpp",
    }.items()
]

TEST_DEPS = [
    ":pinocchio",
    "@boost.test//:boost.test",
    "@eigen//:eigen",
]

COMMON_TEST_HEADERS = [
    "unittest/utils/macros.hpp",
    "unittest/utils/model-generator.hpp",
]

[
    cc_test(
        name = name,
        srcs = [src] + COMMON_TEST_HEADERS,
        deps = TEST_DEPS,
    )
    for name, src in {
        "aba-derivatives_test": "unittest/aba-derivatives.cpp",
        "aba_test": "unittest/aba.cpp",
        "algo-check_test": "unittest/algo-check.cpp",
        "all-joints_test": "unittest/all-joints.cpp",
        "cartesian-product-liegroups_test": "unittest/cartesian-product-liegroups.cpp",
        "center-of-mass-derivatives_test": "unittest/center-of-mass-derivatives.cpp",
        "centroidal-derivatives_test": "unittest/centroidal-derivatives.cpp",
        "centroidal_test": "unittest/centroidal.cpp",
        "cholesky_test": "unittest/cholesky.cpp",
        "com_test": "unittest/com.cpp",
        "compute-all-terms_test": "unittest/compute-all-terms.cpp",
        "constraint_test": "unittest/constraint.cpp",
        "contact-dynamics-derivatives_test": "unittest/contact-dynamics-derivatives.cpp",
        "contact-dynamics_test": "unittest/contact-dynamics.cpp",
        "copy_test": "unittest/copy.cpp",
        "crba_test": "unittest/crba.cpp",
        "data_test": "unittest/data.cpp",
        "eigen-basic-op_test": "unittest/eigen-basic-op.cpp",
        "eigen-tensor_test": "unittest/eigen-tensor.cpp",
        "energy_test": "unittest/energy.cpp",
        "explog_test": "unittest/explog.cpp",
        "finite-differences_test": "unittest/finite-differences.cpp",
        "frames-derivatives_test": "unittest/frames-derivatives.cpp",
        "frames_test": "unittest/frames.cpp",
        "joint-composite_test": "unittest/joint-composite.cpp",
        "joint-configurations_test": "unittest/joint-configurations.cpp",
        "joint-free-flyer_test": "unittest/joint-free-flyer.cpp",
        "joint-generic_test": "unittest/joint-generic.cpp",
        "joint-jacobian_test": "unittest/joint-jacobian.cpp",
        "joint-mimic_test": "unittest/joint-mimic.cpp",
        "joint-planar_test": "unittest/joint-planar.cpp",
        "joint-prismatic_test": "unittest/joint-prismatic.cpp",
        "joint-revolute_test": "unittest/joint-revolute.cpp",
        "joint-spherical_test": "unittest/joint-spherical.cpp",
        "joint-translation_test": "unittest/joint-translation.cpp",
        "kinematics-derivatives_test": "unittest/kinematics-derivatives.cpp",
        "kinematics_test": "unittest/kinematics.cpp",
        "liegroups_test": "unittest/liegroups.cpp",
        "macros_test": "unittest/macros.cpp",
        "model_test": "unittest/model.cpp",
        "quaternion_test": "unittest/quaternion.cpp",
        "regressor_test": "unittest/regressor.cpp",
        "rnea-derivatives_test": "unittest/rnea-derivatives.cpp",
        "rnea-second-order-derivatives_test": "unittest/rnea-second-order-derivatives.cpp",
        "rnea_test": "unittest/rnea.cpp",
        "rotation_test": "unittest/rotation.cpp",
        "rpy_test": "unittest/rpy.cpp",
        "sample-models_test": "unittest/sample-models.cpp",
        "symmetric_test": "unittest/symmetric.cpp",
        "vector_test": "unittest/vector.cpp",
        "version_test": "unittest/version.cpp",
        "visitor_test": "unittest/visitor.cpp",
    }.items()
]
